/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Web;

import Entity.Artist;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author theInterloper
 */
class cookieControl {

    static void writeArtistCookie(HttpServletResponse response, Artist artist) {
        response.setContentType("text/html;charset=UTF-8"); 
        
        int ID =            artist.getID();
        String USERNAME =   artist.getUSERNAME();
        String FIRSTNAME =  artist.getFIRSTNAME();
        String SECONDNAME = artist.getSECONDNAME();
        String EMAIL =      artist.getEMAIL();
        String PASSWORD =   artist.getPASSWORD();
        String ADDRESS1 =   artist.getADDRESS1();
        String ADDRESS2 =   artist.getADDRESS2();
        String ADDRESS3 =   artist.getADDRESS3();
        String PHONE =      artist.getPHONE();
        String DESCRIPTION =artist.getDESCRIPTION();
        
        String values = ID + "," 
                + USERNAME + "," 
                + FIRSTNAME + ","
                + SECONDNAME + ","
                + EMAIL + ","
                + PASSWORD + ","
                + ADDRESS1 + ","
                + ADDRESS2 + ","
                + ADDRESS3 + ","
                + PHONE + ","
                + DESCRIPTION;
        
        String cookieName = "com.crafty.artist";
        
        Cookie lastVisitCookie = new Cookie(cookieName, values);
        lastVisitCookie.setMaxAge(3600);
        response.addCookie(lastVisitCookie);
        System.out.println("The value of the commited cookie is: " + values);
    }

    static String readArtistCookie(HttpServletResponse response, HttpServletRequest request) {
        
        response.setContentType("text/html;charset=UTF-8"); 
        
        String cookieName = "com.crafty.artist";
        
        System.out.println("The cookie is being checked");
        
        //int cookieVal = 0;
        String lastLogin = "no data";
        Cookie[] cookies = request.getCookies();
        System.out.println("The found cookies are: " + cookies + "\n");
        if(cookies != null){
            for(Cookie cookie: cookies){
                if(cookieName.equals(cookie.getName())){
                    lastLogin = cookie.getValue();
                    System.out.println("The value of the last login is: " + lastLogin);
                } 
            }
        }
        System.out.println("The returned value is: " + lastLogin);
        return lastLogin;
    }
    
}
